/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain (a) copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.ohos.core;

import io.sentry.ILogger;
import io.sentry.SentryLevel;
import io.sentry.ohos.core.util.Log;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * OhosLogger
 *
 * @since 2021-06-01
 */
final class OhosLogger implements ILogger {
    private static final String TAG = "Sentry";

    @SuppressWarnings("AnnotateFormatMethod")
    @Override
    public void log(
        final @NotNull SentryLevel level,
        final @NotNull String message,
        final @Nullable Object... args) {
        Log.d(TAG, String.format(message, args), toLogcatLevel(level));
    }

    @SuppressWarnings("AnnotateFormatMethod")
    @Override
    public void log(
        final @NotNull SentryLevel level,
        final @Nullable Throwable throwable,
        final @NotNull String message,
        final @Nullable Object... args) {
        log(level, String.format(message, args), throwable);
    }

    @Override
    public void log(
        final @NotNull SentryLevel level,
        final @NotNull String message,
        final @Nullable Throwable throwable) {
        switch (level) {
            case INFO:
                Log.i(TAG, message, throwable);
                break;
            case WARNING:
                Log.w(TAG, message, throwable);
                break;
            case ERROR:
                Log.e(TAG, message, throwable);
                break;
            case FATAL:
            case DEBUG:
            default:
                Log.d(TAG, message, throwable);
                break;
        }
    }

    @Override
    public boolean isEnabled(@Nullable SentryLevel level) {
        return true;
    }

    private int toLogcatLevel(final @NotNull SentryLevel sentryLevel) {
        switch (sentryLevel) {
            case INFO:
                return Log.INFO;
            case WARNING:
                return Log.WARN;
            case FATAL:
                return Log.ASSERT;
            case DEBUG:
            default:
                return Log.DEBUG;
        }
    }
}
